using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProduceBasicData.Api.Application.Command.WorkOrderProgress;
using ProduceBasicData.Api.DTO;
using ProduceBasicData.ErrorCode;

namespace ProduceBasicData.Api.Controllers
{
    /// <summary>
    /// 工单实时进度控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WorkOrderProgressController : ControllerBase
    {
        private readonly IMediator mediator;

        public WorkOrderProgressController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 获取工单实时进度数据接口
        /// </summary>
        /// <param name="request">查询参数</param>
        /// <returns>工单实时进度数据</returns>
        [HttpGet]
        public async Task<ApiResult<WorkOrderProgressChartDto>> GetWorkOrderProgress([FromQuery] GetWorkOrderProgressCommand request)
        {
            return await mediator.Send(request);
        }

        /// <summary>
        /// 获取工单实时进度数据接口（简化版本，无参数）
        /// </summary>
        /// <returns>工单实时进度数据</returns>
        [HttpGet]
        public async Task<ApiResult<WorkOrderProgressChartDto>> GetWorkOrderProgressSimple()
        {
            var request = new GetWorkOrderProgressCommand
            {
                SortType = 4, // 按创建时间
                SortDirection = 2, // 降序
                PageSize = 10,
                IncludeChartData = true,
                OnlyInProgress = false
            };
            return await mediator.Send(request);
        }

        /// <summary>
        /// 获取进行中的工单进度数据接口
        /// </summary>
        /// <returns>进行中的工单进度数据</returns>
        [HttpGet]
        public async Task<ApiResult<WorkOrderProgressChartDto>> GetInProgressWorkOrders()
        {
            var request = new GetWorkOrderProgressCommand
            {
                SortType = 3, // 按进度
                SortDirection = 2, // 降序
                PageSize = 20,
                IncludeChartData = true,
                OnlyInProgress = true
            };
            return await mediator.Send(request);
        }

        /// <summary>
        /// 获取本月工单进度数据接口
        /// </summary>
        /// <returns>本月工单进度数据</returns>
        [HttpGet]
        public async Task<ApiResult<WorkOrderProgressChartDto>> GetCurrentMonthWorkOrderProgress()
        {
            var startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            var endDate = DateTime.Now;

            var request = new GetWorkOrderProgressCommand
            {
                StartDate = startDate,
                EndDate = endDate,
                SortType = 4, // 按创建时间
                SortDirection = 2, // 降序
                PageSize = 0, // 不分页
                IncludeChartData = true,
                OnlyInProgress = false
            };
            return await mediator.Send(request);
        }

        /// <summary>
        /// 获取特定工单的进度数据接口
        /// </summary>
        /// <param name="orderNo">工单号</param>
        /// <returns>特定工单的进度数据</returns>
        [HttpGet]
        public async Task<ApiResult<WorkOrderProgressChartDto>> GetWorkOrderProgressByOrderNo(string orderNo)
        {
            var request = new GetWorkOrderProgressCommand
            {
                OrderNo = orderNo,
                SortType = 4, // 按创建时间
                SortDirection = 2, // 降序
                PageSize = 1,
                IncludeChartData = true,
                OnlyInProgress = false
            };
            return await mediator.Send(request);
        }
    }
}
